/* jshint esversion:6 */

/**
 * @author: errorcpp@qq.com
 */

var xwindow = ("undefined" != typeof(window)) ? window : global;
xwindow.xlib = xwindow.xlib || {};
let xlib = xwindow.xlib;

// makenum64  makenum64asnum  tohexstring 

/**
 * @brief: 将两个U32合成一个U64的number（！！！超过number范围会丢失精度）
 * @param {Number} h32: 高32位
 * @param {Number} l32: 低32位
 */
xlib.NumberFrom2U32 = function(h32, l32) {
    h32 = parseInt(h32);
    l32 = parseInt(l32);

    let l_str = l32.toString(16);
    // 补满4字节
    for(let n = 8 - l_str.length; n > 0; --n) {
        l_str = "0" + l_str;
    }

    let num = Number("0x" + h32.toString(16) + l_str);

    return num;
};

/**
 * @brief: 将一个number(U64)转为两个U32
 * @param {Number} num: 待转换的数字
 * @return {h32,l32}: 返回高低两个字节
 */
xlib.NumberTo2U32 = function (num) {
    num = parseInt(num);
    let ret = {h32 : 0, l32 : 0};
    
    let num_str = num.toString(16);
    let h_str   = "";
    let l_str   = "";
    if(num_str.length > 8) {
        h_str = num_str.substr(0, num_str.length - 8);
    }

    l_str = num_str.substr(-8);
    
    ret.h32 = Number("0x" + h_str);
    ret.l32 = Number("0x" + l_str);

    return ret;
};

/*
let hlp = xlib.NumberTo2U32(222222222221234);
let n   = xlib.NumberFrom2U32(hlp.h32, hlp.l32);
console.log(n);
*/

/**
 * @brief: 64位数值类型。
 *   !!!模拟的int64实际取值范围是-(2^53) <= N <= 2^53
 * https://github.com/dcodeIO/long.js/blob/master/src/long.js
 * 
 * @param {Number} h32: 高32位
 * @pram 
 */
xlib.Num64 = function (h32, l32, unsigned) {
    

    this._low  = low;
    this._high = high;
};

xlib.Num64.prototype.value = function () {
    let _h  = this._high.toString(16);
    let _hd = 8 - _h.length;
    if(_hd > 0) {
        for(let n = 0; n < _hd; ++n) {
            _h = '0' + _h;
        }
    }

    let _l  = this._low.toString(16);
    let _ld = 8 - _l.length;
    if(_ld > 0) {
        for(let n = 0; n < _ld; ++n) {
            _l = '0' + _l;
        }
    }

    //this._value = Number('0x' + _h + _l);

    return this._value;
};

xlib.Num64.MAX_VALUE = Math.pow(2,  53);
xlib.Num64.MIN_VALUE = Math.pow(-2, 53);

xlib.Num64.UintTest = function() {
    //let n = new xlib.Num64(-1, 0xffffffff);
    //console.log(n);
    //console.log(n.value());
    
};

if(xlib.__enable_auto_unit_test)
    xlib.Num64.UintTest();
